﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Users.IDomain;
using Utility.Extensions;

namespace Users.Domain
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfo : IUserInfo
    {
        Users.Contracts.IQuick_UsersRepository m_IQuick_UsersRepository;
        public UserInfo(Users.Contracts.IQuick_UsersRepository quick_UsersRepository)
        {
            m_IQuick_UsersRepository = quick_UsersRepository;
        }

        /// <summary>
        /// 根据登录信息获取用户数据
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns>
        /// Item1 : 是否成功登录
        /// Item2 : 错误信息
        /// Item3 : 登录成功的User.Id
        /// </returns>
        public async Task<(bool, string,string)> Login(Users.DTO.User loginInfo)
        {
            bool result = false;
            string msg = "";
            loginInfo.Username = loginInfo.Username.StrTrim();
            var userModel = await m_IQuick_UsersRepository.Queryable().Where(t =>
                                           string.Equals(t.U_LoginName, loginInfo.Username, StringComparison.OrdinalIgnoreCase)
                                        || string.Equals(t.U_Phone, loginInfo.Username, StringComparison.OrdinalIgnoreCase)
                                        || string.Equals(t.U_EMail, loginInfo.Username, StringComparison.OrdinalIgnoreCase))
                                        .Where(t => t.IsActive == "Y").FirstAsync();
            if (null == userModel)
            {
                msg = "用户名不存在";
                return (result, msg,"");
            }
            if (!userModel.U_Pwd.Equals(loginInfo.Password.Encrypt(userModel.U_SaltKey)))
            {
                msg = "用户密码不匹配";
                return (result, msg, "");
            }
            if (!"正常".Equals(userModel.U_StateCode))//帐号被锁定
            {
                msg = "用户帐号被锁定";
                return (result, msg, "");
            }
            result = true;

            _ = await m_IQuick_UsersRepository.Updateable(new { ID = userModel.ID, U_Last_LoginTime = DateTime.Now }).ExecuteCommandAsync();

            return (result, msg, userModel.ID);
        }
        /// <summary>
        /// 根据用户ID获取用户登录信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Users.Entity.Quick_Users> GetUserInfo(string id)
        {
            return await m_IQuick_UsersRepository.Queryable(id).FirstAsync();
        }

        /// <summary>
        /// 根据用户ID获取用户登录信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<(bool, string)> UpdatePwd(DTO.UserUpdatePwd userUpdatePwd)
        {
            bool result = false;
            string msg = "";
            var userModel = await m_IQuick_UsersRepository.Queryable(userUpdatePwd.ID).FirstAsync();
            if (null == userModel)
            {
                msg = "用户名不存在";
                return (result, msg);
            }
            if (!userModel.U_Pwd.Equals(userUpdatePwd.OldPassword.Encrypt(userModel.U_SaltKey)))
            {
                msg = "用户密码不正确";
                return (result, msg);
            }
            string saltkey;
            string newPwd = Utility.Encrypt.StringEncrypt.Encrypt(userUpdatePwd.Password, out saltkey);
            result = await m_IQuick_UsersRepository.Updateable(new
            {
                ID = userModel.ID,
                U_Pwd = newPwd,
                U_SaltKey = saltkey,
                ModifyBy = userModel.ID,
                ModifyOn = DateTime.Now
            }).ExecuteCommandAsync() > 0;
            msg = "密码更新成功!";
            return (result, msg);
        }
    }
}
